//
//  JYTPlaceholderTextView.m
//  JYT_IOS
//
//  Created by nxa-tankeke on 17/1/17.
//  Copyright © 2017年 nxa-yuanguoxiang. All rights reserved.
//

#import "JYTPlaceholderTextView.h"
#define YGXNoteCenter [NSNotificationCenter defaultCenter]


@implementation JYTPlaceholderTextView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.font = [UIFont systemFontOfSize:12];
        self.placeholderColor = [UIColor grayColor];
        self.tColor = [UIColor colorWithRed:96 / 255.0 green:96 / 255.0 blue:96 / 255.0 alpha:1];
        
        [YGXNoteCenter addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}

- (void)dealloc
{
    [YGXNoteCenter removeObserver:self];
}


- (void)textDidChange
{
    [self setNeedsDisplay];
}


- (void)drawRect:(CGRect)rect {
    if (self.hasText) return;
    
    rect.origin.x = 4;
    rect.origin.y = 7;
    rect.size.width -= 2 * rect.origin.x;
    
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = self.font;
    attrs[NSForegroundColorAttributeName] = self.tColor;
    [self.placeholder drawInRect:rect withAttributes:attrs];
}


- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
    _placeholderColor = placeholderColor;
    
    [self setNeedsDisplay];
}

- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = [placeholder copy];
    
    [self setNeedsDisplay];
}

- (void)setFont:(UIFont *)font
{
    [super setFont:font];
    
    [self setNeedsDisplay];
}

- (void)setText:(NSString *)text
{
    [super setText:text];
    
    [self setNeedsDisplay];
}

- (void)setAttributedText:(NSAttributedString *)attributedText
{
    [super setAttributedText:attributedText];
    
    [self setNeedsDisplay];
}
-(void)setTColor:(UIColor *)tColor
{
    _tColor = tColor;
    
    [self setNeedsDisplay];
}


@end
